package com.siyu.tickets.dao.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.siyu.common.hibernate3.Finder;
import com.siyu.common.hibernate3.HibernateBaseDao;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.PartnerDao;
import com.siyu.tickets.entity.Partner;

@Repository
public class PartnerDaoImpl extends HibernateBaseDao<Partner, Integer> implements PartnerDao {

	public Pagination getPage(String name, Boolean enable, int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from Partner bean");
		f.append(" where 1=1");
		if (!StringUtils.isBlank(name)) {
			f.append(" and ( bean.title like :name ");
			f.append(" or  bean.user.username like :name ");
			f.append(" or  bean.user.realname like :name ");
			f.append(")");
			f.setParam("name", "%" + name + "%");
		}
		if (enable != null) {
			f.append(" and bean.user.enable=:enable");
			f.setParam("enable", enable);
		}
		f.append(" order by bean.user.username asc");
		return find(f, pageNo, pageSize);
	}

	public Partner findById(Integer id) {
		Partner entity = get(id);
		return entity;
	}

	public Partner save(Partner bean) {
		getSession().save(bean);
		return bean;
	}

	public Partner deleteById(Integer id) {
		Partner entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	protected Class<Partner> getEntityClass() {
		return Partner.class;
	}

}
